; RUN: opt -passes=reassociate -S < %s | FileCheck %s

;------------------------------------------------------------------------------
; CASE 1: (A&B)&~A == 0
;------------------------------------------------------------------------------
define i32 @test1(i32 %a, i32 %b) {
; CHECK-LABEL: @test1(
; CHECK-NEXT:    ret i32 0
;
  %t2 = and i32 %b, %a
  %t4 = xor i32 %a, -1
  %t5 = and i32 %t2, %t4
  ret i32 %t5
}

;------------------------------------------------------------------------------
; CASE 2: (b+(a+1234))+-a -> b+1234
;------------------------------------------------------------------------------
define i32 @test2(i32 %b, i32 %a) {
; CHECK-LABEL: @test2(
; CHECK:    [[T5:%.*]] = add i32 %b, 1234
; CHECK-NEXT:    ret i32 [[T5]]
;
  %t1 = add i32 %a, 1234
  %t2 = add i32 %b, %t1
  %t4 = sub i32 0, %a
  %t5 = add i32 %t2, %t4
  ret i32 %t5
}

;------------------------------------------------------------------------------
; CASE 3: A - 123 + 123 = A - 0
;------------------------------------------------------------------------------
define i32 @test3(i32 %arg) {
; CHECK-LABEL: @test3(
; CHECK-NEXT:    [[ARG_NEG:%.*]] = sub i32 0, %arg
; CHECK-NEXT:    ret i32 [[ARG_NEG]]
;
  %tmp1 = sub i32 -123, %arg
  %tmp2 = add i32 %tmp1, 123
  ret i32 %tmp2
}
